package com.woniuxy.microsysserver.controller;

import com.woniuxy.microsysserver.bean.AdminInfo;
import com.woniuxy.microsysserver.service.IAdminService;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RequestMapping("/redis")
@RestController
public class RedisController {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ValueOperations valueOperations;

    @Resource
    private ListOperations listOperations;

    @Resource
    private ZSetOperations zSetOperations;

    @Resource
    private HashOperations hashOperations;

    @Resource
    private IAdminService adminServiceImpl;


    @RequestMapping("/packFull")
    public void string(){

        if (redisTemplate.hasKey("admins")){
            System.out.println("has admins");
            return;
        }

        List<AdminInfo> allAdmins = adminServiceImpl.findAll();
        Map<String,Object> admins = new HashMap();

        for(int i = 0; i < allAdmins.size(); i++){
            admins.put(""+allAdmins.get(i).getId(),allAdmins.get(i));
        }

        hashOperations.putAll("admins",admins);

        redisTemplate.expire("admins",30, TimeUnit.MINUTES);
    }

}
